package br.com.sis.rh.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.sis.rh.entity.InstituicaoEnsino;
import br.com.sis.rh.facade.InstituicaoGraduacaoFacade;
import br.com.sis.rh.vo.RetornoAjaxVO;

@SuppressWarnings("unchecked")
@Controller
@Scope("request")
@ParentPackage("json-default")
@Namespace(value = "/")
public class InstituicaoGraduacaoAction implements ComumAction {

    @Autowired
    private EhCacheCacheManager ehCache;
	
	@Autowired
	private InstituicaoGraduacaoFacade instituicaoGraduacaoFacade;
	
	private RetornoAjaxVO retornoAjax;
	
	@Override
	public String incluir() {
		return null;
	}

	@Override
	public String alterar() {
		return null;
	}

	@Override
	public String excluir() {
		return null;
	}

	@Override
	@Action(value="listarInstituicaoGraduacao", results={
			@Result(name="success", type="json", params={"root","retornoAjax"})
	})
	public String listar() {
		retornoAjax = new RetornoAjaxVO();

		List<InstituicaoEnsino> instituicaoGraduacaoCache = new ArrayList<>();
		
		if(ehCache.getCache("instituicaoGraduacao").get("1") != null) {
			instituicaoGraduacaoCache = (ArrayList<InstituicaoEnsino>) ehCache.getCache("instituicaoGraduacao").get("1").get();
		} else {
			instituicaoGraduacaoCache = instituicaoGraduacaoFacade.listar(new InstituicaoEnsino());
			ehCache.getCache("instituicaoGraduacao").put("1", instituicaoGraduacaoCache);
		}
		
		retornoAjax.setObjetoRetornado(instituicaoGraduacaoCache);
		
		return SUCCESS;
	}

	public RetornoAjaxVO getRetornoAjax() {
		return retornoAjax;
	}

	public void setRetornoAjax(RetornoAjaxVO retornoAjax) {
		this.retornoAjax = retornoAjax;
	}

}
